import json
import re

# 1.统计 Mashup 中的包含 Web API 个数、Web API 被使用的次数和 Web API 提供商发布 Web API 的个数
print("统计 Mashup 中的包含 Web API 个数、Web API 被使用的次数和 Web API 提供商发布 Web API 的个数")
print("数据来自 active_mashups_data.txt、deadpool_mashups_data.txt")
print("api_version_accessbiliby-1.txt、api_version_accessbiliby-2.txt、api_version_accessbiliby-3.txt、api_version_accessbiliby-4.txt、api_version_accessbiliby-5.txt")

mashupTitleList = []
apiCountList = []
apiUsedCountList = {}
providerAndCount = {}

txtFile1 = open(r"../data/raw/api_mashup/active_mashups_data.txt")
txtFile2 = open(r"../data/raw/api_mashup/deadpool_mashups_data.txt")
txtContentMashupList = [txtFile1.read(), txtFile2.read()]

txtAPIFile1 = open(r"../data/raw/accessibility/api_accessibility/api_version_accessbiliby-1.txt")
txtAPIFile2 = open(r"../data/raw/accessibility/api_accessibility/api_version_accessbiliby-2.txt")
txtAPIFile3 = open(r"../data/raw/accessibility/api_accessibility/api_version_accessbiliby-3.txt")
txtAPIFile4 = open(r"../data/raw/accessibility/api_accessibility/api_version_accessbiliby-4.txt")
txtAPIFile5 = open(r"../data/raw/accessibility/api_accessibility/api_version_accessbiliby-5.txt")
txtContentAPIList = [txtAPIFile1.read(), txtAPIFile2.read(), txtAPIFile3.read(), txtAPIFile4.read(), txtAPIFile5.read()]

# 1.Mashup 中的包含 Web API 个数
# 2.Web API 被使用的次数
for txtContent in txtContentMashupList:
    jsondata = json.loads(txtContent)

    for mashup in jsondata:
        if mashup != None:
            mashupTitleList.append(mashup["title"][8:])
            related_apis = mashup["related_apis"]
            apiCountList.append(len(related_apis))
            for api in related_apis:
                if api != None:
                    if api["title"] in apiUsedCountList:
                        apiUsedCountList[api["title"]] += 1
                    else:
                        apiUsedCountList[api["title"]] = 1

regular = re.compile('https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+')
# 3.Web API 提供商发布 Web API 的个数
for txtContent in txtContentAPIList:
    jsondata = json.loads(txtContent)

    for api in jsondata:
        if api != None:
            visit_status = api["visit_status"]
            if visit_status != None and len(visit_status) > 0:
                visit_url = visit_status[0]["visit_url"]
                provider = re.findall(regular, visit_url)
                if provider != None and len(provider) > 0:
                    if provider[0] in providerAndCount:
                        providerAndCount[provider[0]] += 1
                    else:
                        providerAndCount[provider[0]] = 1

print("--------------------------------Mashup 中的包含 Web API 个数---------------------------------")
mashupApiCount = zip(mashupTitleList, apiCountList)
for item in mashupApiCount:
    print(item)

print("-----------------------------------Web API 被使用的次数--------------------------------------")
tempList = sorted(apiUsedCountList.items(), key=lambda x:x[1], reverse=True)
for item in tempList:
    print(item)

print("-----------------------------Web API 提供商发布 Web API 的个数--------------------------------")
tempList = sorted(providerAndCount.items(), key=lambda x:x[1], reverse=True)
for item in tempList:
    print(item)